﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Sector7.Restaurant.Biz;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Framework.Utility;
using System.IO;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// CompanyInformationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CompanyInformationWindow : UserControl
    {
        public CompanyInformationWindow()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择餐厅标志";
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "PNG图像文件 (*.png)|*.png|GIF图像文件 (*.gif)|*.gif|JPEG图像文件 (*.jpg, *.jpeg)|*.jpg;*.jpeg|所有文件|*.*";

            bool? result = openFileDialog.ShowDialog();
            if (true == result)
            {
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = File.OpenRead(openFileDialog.FileName) as Stream;
                bitmapImage.EndInit();

                imgPreview.Source = bitmapImage;

                byte[] data = new byte[bitmapImage.StreamSource.Length];
                bitmapImage.StreamSource.Seek(0, SeekOrigin.Begin);
                bitmapImage.StreamSource.Read(data, 0, data.Length);

                Environments.CurrentCompany.GetType().GetProperty("Logo").SetValue(Environments.CurrentCompany, data, null);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = Environments.CurrentCompany;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(S7_Company));
            delta.Conditions.Add("UniqueId", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "UniqueId"));
            delta.DeltaProperties.Add("Name", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Name"));
            delta.DeltaProperties.Add("Introduce", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Introduce"));
            delta.DeltaProperties.Add("Address", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Address"));
            delta.DeltaProperties.Add("Postcode", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Postcode"));
            delta.DeltaProperties.Add("Telephone", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Telephone"));
            delta.DeltaProperties.Add("Logo", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Logo"));
            delta.DeltaProperties.Add("Map", ReflectionUtil.GetPropertyValue(Environments.CurrentCompany, "Map"));

            Aide.LocalClient.Invoke<int>(
                service => service.SetCompany(delta));
        }
    }
}
